#!/bin/bash

# 检查参数数量
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <commit_original_id> <new_date>"
    exit 1
fi

# 参数
COMMIT_ORIGINAL_ID=$1
NEW_DATE=$2
GIT_FILTER_REPO_PATH="./git_filter_repo.py"

# 验证 git-filter-repo 是否存在
if [ ! -f "$GIT_FILTER_REPO_PATH" ]; then
    echo "Error: git-filter-repo not found at $GIT_FILTER_REPO_PATH"
    exit 1
fi

# 转换为 Unix 时间戳
timestamp=$(date -d "$NEW_DATE" +%s)
# 添加时区偏移量
formatted_time="$timestamp +0800"
echo "$formatted_time"

# 执行 git-filter-repo
python "$GIT_FILTER_REPO_PATH" --preserve-commit-hashes --commit-callback "
if commit.original_id == b'$COMMIT_ORIGINAL_ID':
    commit.author_date = b'$formatted_time'
    commit.committer_date = b'$formatted_time'
"

# 检查结果
if [ $? -eq 0 ]; then
    echo "Commit information modified successfully."
else
    echo "Error occurred while modifying commit information."
fi

# 记得 GIT_FILTER_REPO_PATH="./git_filter_repo.py"
# 输入格式是 "年-月-日 时:分:秒"
# sh modifyTimesByHash.sh 268f9926c94f563be11660ec928e085d0bede101 "2023-13-32 21:15:55"